import Vue from 'vue'
import Router from 'vue-router'

// 导入其他路由模块
import permissionRouter from './modules/permission'
import structureRouter from './modules/structure'
import usersRouter from './modules/users'
import roleRouter from './modules/role'
import equipRouter from './modules/equip'
import lendRouter from './modules/lend'
import monitorRouter from './modules/monitor'
import controlRouter from './modules/control'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 动态路由
export const asyncRoutes = [
  permissionRouter,
  structureRouter,
  usersRouter,
  roleRouter,
  equipRouter,
  lendRouter,
  monitorRouter,
  controlRouter,
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'home' }
    },
    {
      path: 'detail/:id', // query传参 动态路由传参
      component: () => import('@/views/users/components/userdetail'),
      hidden: true, // 不在左侧菜单显示
      meta: {
        title: '用户详情'
      }
    }
    ]
  }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  base: '/wulain/', // 配置项目的基础地址
  scrollBehavior: () => ({ y: 0 }), // 管理滚动行为 如果出现滚动 切换就让 让页面回到顶部
  routes: [...constantRoutes] // 临时合并所有的路由
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
